package cc.rengu.igas.channel.allinpay.biz.impl;

import cc.rengu.igas.channel.allinpay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.allinpay.facade.AllinpayAuthenticationService;
import cc.rengu.igas.channel.allinpay.facade.request.AllinpayFiveFactorAuthenticationRequest;
import cc.rengu.igas.channel.allinpay.facade.request.AllinpayFourFactorAuthenticationRequest;
import cc.rengu.igas.channel.allinpay.facade.response.AllinpayFiveFactorAuthenticationResponse;
import cc.rengu.igas.channel.allinpay.facade.response.AllinpayFourFactorAuthenticationResponse;
import cc.rengu.igas.channel.allinpay.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class AllinpayAuthenticationServiceImpl implements AllinpayAuthenticationService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 通联五要素鉴权
     *
     * @param allinpayFiveFactorAuthenticationRequest 通联五要素鉴权请求
     * @return 签权结果
     */
    @Override
    public Result<AllinpayFiveFactorAuthenticationResponse> AllinpayFiveFactorAuthentication(AllinpayFiveFactorAuthenticationRequest allinpayFiveFactorAuthenticationRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AllinpayFiveFactorAuthenticationResponse> result = new Result<>();
        AllinpayFiveFactorAuthenticationResponse response = new AllinpayFiveFactorAuthenticationResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(allinpayFiveFactorAuthenticationRequest));
            DubboResult dubboResult = dubboService.callBizService(allinpayFiveFactorAuthenticationRequest, AllinpayFiveFactorAuthenticationResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (AllinpayFiveFactorAuthenticationResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("通联五要素签权失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    /**
     * 通联四要素鉴权
     *
     * @param allinpayFourFactorAuthenticationRequest 通联四要素鉴权请求
     * @return 签权结果
     */
    @Override
    public Result<AllinpayFourFactorAuthenticationResponse> AllinpayFourFactorAuthentication(AllinpayFourFactorAuthenticationRequest allinpayFourFactorAuthenticationRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AllinpayFourFactorAuthenticationResponse> result = new Result<>();
        AllinpayFourFactorAuthenticationResponse response = new AllinpayFourFactorAuthenticationResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(allinpayFourFactorAuthenticationRequest));
            DubboResult dubboResult = dubboService.callBizService(allinpayFourFactorAuthenticationRequest, AllinpayFourFactorAuthenticationResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (AllinpayFourFactorAuthenticationResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("通联四要素签权失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }
}
